package com.springframework.beans.factory.config;

import com.springframework.beans.MyBeanMetadataElement;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @Author 虎哥
 * @Description 包含 BeanDefinition、Bean 的名称以及别名（支持多个）
 * 在解析出来 BeanDefinition 后都会转换成 BeanDefinitionHolder 对象，然后进行注册
 * <p>
 * 要带着问题去学习,多猜想多验证
 **/
public class MyBeanDefinitionHolder implements MyBeanMetadataElement {
    private final MyBeanDefinition beanDefinition;

    private final String beanName;

    @Nullable
    private final String[] aliases;


    /**
     * Create a new BeanDefinitionHolder.
     * @param beanDefinition the BeanDefinition to wrap
     * @param beanName the name of the bean, as specified for the bean definition
     */
    public MyBeanDefinitionHolder(MyBeanDefinition beanDefinition, String beanName) {
        this(beanDefinition, beanName, null);
    }

    /**
     * Create a new BeanDefinitionHolder.
     * @param beanDefinition the BeanDefinition to wrap
     * @param beanName the name of the bean, as specified for the bean definition
     * @param aliases alias names for the bean, or {@code null} if none
     */
    public MyBeanDefinitionHolder(MyBeanDefinition beanDefinition, String beanName, @Nullable String[] aliases) {
        Assert.notNull(beanDefinition, "BeanDefinition must not be null");
        Assert.notNull(beanName, "Bean name must not be null");
        this.beanDefinition = beanDefinition;
        this.beanName = beanName;
        this.aliases = aliases;
    }

    /**
     * Copy constructor: Create a new BeanDefinitionHolder with the
     * same contents as the given BeanDefinitionHolder instance.
     * <p>Note: The wrapped BeanDefinition reference is taken as-is;
     * it is {@code not} deeply copied.
     * @param beanDefinitionHolder the BeanDefinitionHolder to copy
     */
    public MyBeanDefinitionHolder(MyBeanDefinitionHolder beanDefinitionHolder) {
        Assert.notNull(beanDefinitionHolder, "BeanDefinitionHolder must not be null");
        this.beanDefinition = beanDefinitionHolder.getBeanDefinition();
        this.beanName = beanDefinitionHolder.getBeanName();
        this.aliases = beanDefinitionHolder.getAliases();
    }


    /**
     * Return the wrapped BeanDefinition.
     */
    public MyBeanDefinition getBeanDefinition() {
        return this.beanDefinition;
    }

    /**
     * Return the primary name of the bean, as specified for the bean definition.
     */
    public String getBeanName() {
        return this.beanName;
    }

    /**
     * Return the alias names for the bean, as specified directly for the bean definition.
     * @return the array of alias names, or {@code null} if none
     */
    @Nullable
    public String[] getAliases() {
        return this.aliases;
    }

    /**
     * Expose the bean definition's source object.
     * @see BeanDefinition#getSource()
     */
    @Override
    @Nullable
    public Object getSource() {
        return this.beanDefinition.getSource();
    }

    /**
     * Determine whether the given candidate name matches the bean name
     * or the aliases stored in this bean definition.
     */
    public boolean matchesName(@Nullable String candidateName) {
        return (candidateName != null && (candidateName.equals(this.beanName) ||
                candidateName.equals(BeanFactoryUtils.transformedBeanName(this.beanName)) ||
                ObjectUtils.containsElement(this.aliases, candidateName)));
    }


    /**
     * Return a friendly, short description for the bean, stating name and aliases.
     * @see #getBeanName()
     * @see #getAliases()
     */
    public String getShortDescription() {
        if (this.aliases == null) {
            return "Bean definition with name '" + this.beanName + "'";
        }
        return "Bean definition with name '" + this.beanName + "' and aliases [" + StringUtils.arrayToCommaDelimitedString(this.aliases) + ']';
    }

    /**
     * Return a long description for the bean, including name and aliases
     * as well as a description of the contained {@link BeanDefinition}.
     * @see #getShortDescription()
     * @see #getBeanDefinition()
     */
    public String getLongDescription() {
        return getShortDescription() + ": " + this.beanDefinition;
    }

    /**
     * This implementation returns the long description. Can be overridden
     * to return the short description or any kind of custom description instead.
     * @see #getLongDescription()
     * @see #getShortDescription()
     */
    @Override
    public String toString() {
        return getLongDescription();
    }



}
